Skip to content

Scope time range options per command and bound profile bucket count - #12

Merged
jackfranklin merged 1 commit into
ChromeDevTools:mainfrom
nucliweb:fix-cli-option-validation
Nov 25, 2026
Merged

jackfranklin merged 1 commit into
ChromeDevTools:mainfrom
nucliweb:fix-cli-option-validation

Conversation

@nucliweb

@nucliweb nucliweb commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

--time-range and --absolute-timestamps are declared only on the commands that use them. threads and scan reject both, and diff rejects --time-range instead of ignoring it. diff keeps --absolute-timestamps for its --window-a and --window-b values. Command help only lists the options each command supports.

tasks --min-duration rejects negative and non-numeric values. An empty value (--min-duration=) still becomes 0, because yargs converts it before coerce runs, like the other number options.

extractProfile() rejects bucket sizes below 0.01ms and more than 1000 buckets, and the error shows the smallest valid bucket size. The bucket size is rounded before the check, so the check matches the rounded loop step. Before, --bucket 0.01ms created 16,000 buckets on the 160ms fixture, and a 10s trace would create 1,000,000.

Tests cover each rejected option, the bucket limits, the rounded bucket size and the exact 1000-bucket boundary. Verified with npm run check, npm run build, npm test and the built CLI, including --time-range and --absolute-timestamps on every command that declares them.

@mangeshraut712 mangeshraut712 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scoping the time-range options per command and rejecting them elsewhere will be a lot clearer for agents than silently ignoring them. The extracted loadTraceWithTimeRange helper also cuts down a lot of repetition.

Comment thread src/cli.ts Outdated
Comment on lines +73 to +80
function positiveDuration(flag: string): (value: unknown) => number {
return (value) => {
const ms = parseTimeValue(String(value));
if (ms <= 0) {
throw new Error(`--${flag} must be a duration > 0. Received: ${value}`);
}
return ms;
};

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

> 0 still lets through durations that give an empty result, which is the case this PR is trying to remove. extractProfile advances with currentEnd = Math.min(endMs, round(currentStart + bucketSizeMs)), and round defaults to 2 decimals. So --bucket 1us (0.001 ms) or --bucket 0.004 makes currentEnd === currentStart on the first iteration. It breaks out and returns buckets: [] with exit 0.

At the other end, --bucket 10us is accepted and on a 10 s trace that means ~1M BottomUpRootNode builds. Rejecting anything below 0.01 ms (or whatever resolution round uses) would close the empty-result case. A cap on totalRangeMs / bucket with a clear error might be worth adding too. I worked this out from reading profile.ts and haven't run it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed by running it: --bucket 1us and --bucket 0.004 returned buckets: [] with exit 0, and --bucket 10us returned 16000 buckets on the 160 ms test fixture. Fixed in 2e829a0:

  • --bucket below 0.01ms fails with --bucket must be a duration >= 0.01ms. Received: 1us.
  • profile fails when the range needs more than 1000 buckets and suggests the smallest valid size, for example Bucket size 0.01ms creates 16000 buckets over 160ms. The maximum is 1000. Use a bucket size of at least 0.16ms.

@jackfranklin

Copy link
Copy Markdown
Collaborator

Thanks for putting this together! Scoping --time-range and --absolute-timestamps to the commands that use them and extracting loadTraceWithTimeRange cleans up a lot of duplication.

A few small things I noticed while reviewing:

  1. Empty/whitespace strings and booleans in nonNegativeNumber / positiveInteger (src/cli.ts):
    Because type: 'number' was removed, yargs passes raw untyped values (string, boolean, or Array if repeated) into coerce. In JS, Number(""), Number(" "), and Number(false) (e.g. --min-duration "" or --no-min-duration) evaluate to 0, which passes !Number.isFinite(num) || num < 0. Checking that value is either a number or a non-empty string (typeof value === 'string' && value.trim() !== '') before calling Number(value) will ensure blank strings and booleans are rejected.

  2. Unrounded bucketSizeMs vs. rounded step increments in extractProfile (src/engine/extractors/profile.ts):

    • In the while loop, each step computes round(currentStart + bucketSizeMs) (rounded to 2 decimal places / 0.01ms), whereas bucketSizeMs in the MAX_BUCKETS check is not rounded when passed via options?.bucketMs. For example, --bucket 14us (0.014ms) over a 14ms window passes 0.014 < 14 / 1000, but round(currentStart + 0.014) rounds down to +0.01ms on every iteration and produces 1,400 buckets.
    • MIN_BUCKET_MS (0.01) is exported from profile.ts but only checked in cli.ts, so calling extractProfile() directly with bucketMs: 0.004 on a small range can still bypass MAX_BUCKETS and return buckets: []. Rounding options.bucketMs (or enforcing MIN_BUCKET_MS) inside extractProfile() keeps the check and the loop step consistent.
  3. Preserving yargs types on loadTrace and loadTraceWithTimeRange (src/cli.ts):
    Instead of type CliArgs = Record<string, unknown> with as string / as boolean casts inside the helpers, we can type the parameters structurally ({ file: string; traceEngine?: string } and { file: string; traceEngine?: string; timeRange?: string; absoluteTimestamps: boolean }). Yargs already infers those exact types on args, so it avoids the casts and gives us a compile error if loadTraceWithTimeRange is ever called on a command that didn't register the time-range options.

@nucliweb
nucliweb force-pushed the fix-cli-option-validation branch from 2e829a0 to 651ed0f Compare November 19, 2026 15:29
@nucliweb

Copy link
Copy Markdown
Contributor Author

Thanks for the review! All three points are addressed, and the branch is rebased on main (conflicts with #20 resolved).

  1. toNumber() only converts numbers and non-blank strings, so --min-duration "", whitespace and --no-min-duration are rejected now. --top already rejected these because they became 0, but both helpers share the same guard and there are tests for each case.
  2. extractProfile() rejects bucketMs below MIN_BUCKET_MS and rounds it before the MAX_BUCKETS check. --bucket 14us over a 14ms window fails with "Bucket size 0.01ms creates 1400 buckets over 14ms" instead of producing 1,400 buckets.
  3. loadTrace and loadTraceWithTimeRange take TraceArgs / TimeRangeArgs. I checked that calling loadTraceWithTimeRange from insights fails to compile with "Property 'absoluteTimestamps' is missing".

@nucliweb
nucliweb force-pushed the fix-cli-option-validation branch from 651ed0f to 0386d50 Compare November 24, 2026 17:30
@nucliweb nucliweb changed the title Reject unsupported --time-range and validate numeric options Scope time range options per command and bound profile bucket count Nov 24, 2026
@nucliweb

Copy link
Copy Markdown
Contributor Author

I rebuilt this PR on top of main after #16 and #17 landed. Those PRs already validate result limits and the minimum bucket size, and insights and navigations support --time-range now, so I removed those parts and the loadTraceWithTimeRange refactor.

What remains is the per-command scoping of --time-range and --absolute-timestamps (now only threads, scan and diff reject them), --min-duration validation, and a bucket count limit for profile, which includes your second point from the earlier review. The description has the details.

`--time-range` and `--absolute-timestamps` are declared only on the commands that use them, so `threads` and `scan` reject both and `diff` rejects `--time-range` instead of ignoring them. `diff` keeps `--absolute-timestamps` for `--window-a` and `--window-b`, and command help only lists the options each command supports. `tasks --min-duration` rejects negative and non-numeric values.

`extractProfile()` enforces a 0.01ms minimum and a maximum of 1000 buckets, and rounds the bucket size before the check so it matches the loop step. Before, `--bucket 0.01ms` created 16,000 buckets on a 160ms trace.
@nucliweb
nucliweb force-pushed the fix-cli-option-validation branch from 0386d50 to 11e7f21 Compare November 24, 2026 18:22
@jackfranklin
jackfranklin merged commit ed15ce3 into ChromeDevTools:main Nov 25, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants